<?php
/**
 * Created by PhpStorm.
 * User: admin
 * Date: 2018/6/28
 * Time: 14:30
 */
namespace App\Admin\Controllers;
use App\Modules\Pay\Models\PayWithdrawCash;
use App\Modules\Pay\Services\PayWithdrawCashService;
use GuzzleHttp\Client;
use function GuzzleHttp\default_ca_bundle;
use Illuminate\Http\Request;
use Illuminate\Routing\Controller;
use DB;
use Illuminate\Support\Facades\Log;

class CashController extends Controller {

    public function withDrawList(){
        return view('cash.withdraw');
    }

    public function getWithDrawList(Request $request) {

        $start_time =$request->has('zstart')?$request->input('zstart'):'1970-08-01';
        $end_time =$request->has('zend')?$request->input('zend'):date('Y-m-d');
        $status = $request->has('status')?$request->input('status'):'';
        $filter = array(
            ['pwc.created_at','>=',$start_time?$start_time:'1970-08-01'],
            ['pwc.created_at','<=',$end_time?$end_time:date('Y-m-d')]
        );

        if($status!='unselect'){
            $status = PayWithdrawCashService::getWithdrawCashStatusStr($status);
            $filter[] = ['pwc.status','=',$status];
        }
        $res = DB::table('pay_withdraw_cashes as pwc')
            ->select('pwc.id','pwc.amount','pwc.created_at','pwc.status','u.nickname','u.head_img')
            ->join('users as u','u.id','=','pwc.uid')
            ->orderBy('pwc.created_at','desc')
            ->where($filter)
            ->paginate(15);

        if($res){

            $res = json_decode(json_encode($res,JSON_UNESCAPED_UNICODE),true);
            //var_dump($res);
            foreach ($res['data'] as $key=>$value){
                $status = PayWithdrawCashService::getWithdrawCashIntStatus($value['status']);
                $handle = '';
                switch ($status){
                    case 0:$handle="<button onclick='check({$value['id']})' class='btn btn-info'>审核</button><button onclick='check_refuse({$value['id']})' class='btn btn-danger'>审核不通过</button>";break;
                    case 9:$handle="<button onclick='check({$value['id']})' class='btn btn-info'>重新审核</button>";break;
                    case 41:$handle="<button onclick='check({$value['id']})' class='btn btn-info'>重新审核</button>";break;
                    default:$handle="<button disabled class='btn btn-default'>{$value['status']}</button>";;
                }
                $res['data'][$key]['status_code'] =$handle;
                //PayWithdrawCashService::getWithdrawCashIntStatus($value['status']);
            }
            return [
                'data'=>$res['data'],
                'pagecount'=>ceil($res['total']/$res['per_page'])
            ];
        }

    }

    public function checkWithdraw(Request $request) {
        $action = $request->has('action')?$request->input('action'):'';
        $withdrawId = $request->has('withdrawId')?$request->input('withdrawId'):'';
        if(!$action || !$withdrawId) {
            return response()->error('PARAM_EMPTY');
        }
        try{
            if($action=='check'){
                $PayWithdrawInfo = PayWithdrawCashService::getWithdrawCash( $withdrawId);
                if(!$PayWithdrawInfo){
                    return response()->error('PARAM_ERROR');
                }
                $PayWithdrawInfo->status=PayWithdrawCashService::getWithdrawCashStatusStr(10);
                $PayWithdrawInfo->save();
                $client = new Client(['timeout' => 3.0,]);
                $res = $client->request('post',env('PAY_TO_PERSON_URL'),['query'=>['withdraw_cash_id'=>$withdrawId]]);
                return $res;
            }

            if($action=='refuse_check'){
                $PayWithdrawInfo = PayWithdrawCashService::getWithdrawCash( $withdrawId);
                if(!$PayWithdrawInfo){
                    return response()->error('PARAM_ERROR');
                }
                $PayWithdrawInfo->status=PayWithdrawCashService::getWithdrawCashStatusStr(9);
                $PayWithdrawInfo->save();
                return response()->success();
            }
        }catch (\Exception $e){
            return ['code'=>$e->getCode(),'msg'=>$e->getMessage()];
        }

    }

    public function my_test(){
        return view('cash.testing');
    }
}